using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class TreePanel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridControl.TreeNode root = new GridControl.TreeNode();
        root.id = "0";
        root.leaf = false;
        root.text = "root";
        TreePane.TreeNodes.Add(root);

        //GridControl.Menu menu = new GridControl.Menu();
        //GridControl.MenuItem item = new GridControl.MenuItem();
        //item.Text = "Delete node";
        //item.Id = "deletenode";
        ////item.OnClientClick = "DeleteNode";
        //menu.Items.Add(item);
        //TreePane.ContextMenu = menu;

    }


    protected void TreePane_NodeEdited(object sender, GridControl.NodeEditedEventArgs e)
    {
        //updates the database to reflect the edition

        SqlConnection con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "update tb_tree set text=@text where id=@id";
        cmd.Parameters.AddWithValue("@text", e.NewValue);
        cmd.Parameters.AddWithValue("@id", e.NodeRenamed.id);
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            System.Diagnostics.Debug.Write(ex.ToString());
        }
        finally
        {
            con.Close();
            con.Dispose();
            cmd.Dispose();
        }

    }
    protected void TreePane_NodeMoved(object sender, GridControl.NodeMoveEventArgs e)
    {
        if (e.NewParent.id==e.OldParent.id)//do nothing... node still has the same parent
        {
            return;
        }
        SqlConnection con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "update tb_tree set id_parent=@id_parent where id=@id";
        cmd.Parameters.AddWithValue("@id_parent", e.NewParent.id);
        cmd.Parameters.AddWithValue("@id", e.NodeMoved.id);
        cmd.ExecuteNonQuery();
        con.Close();
        con.Dispose();
        cmd.Dispose();
    }

    protected void TreePane_ContextMenuClicked(object sender, GridControl.TreeContextMenuEventArgs e)
    {
        GridControl.MenuItem it = e.MenuClicked;
        if (it.Id=="deletenode")
        {
            
            //TODO -UPDATE THE DATABASE - do something
        }
    }
}
